package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.common.UserIdThreadLocal;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.mapper.AddressBookMapper;
import com.itheima.reggie.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;



    //删除地址
    @Override
    public R deleteAddressBook(List<Long> ids) {
        addressBookMapper.deleteBatchIds(ids);
        return R.success("删除成功");
    }
    //修改地址
    @Override
    public R updateAddressBook(AddressBook addressBook) {
        addressBookMapper.updateById(addressBook);
        return R.success("修改成功");
    }
    //查询当前用户全部地址

    @Override
    public R getAllAddressBook(HttpSession session) {
        Long userId = UserIdThreadLocal.getId();
        List<AddressBook> list = addressBookMapper.getAllAddressBook(userId);
        return R.success(list);
    }


    //查询默认地址
    @Override
    public R getDefaultAddressBook(HttpSession session) {
        //获取当前用户id
        Long userId = (Long) session.getAttribute("userId");
        AddressBook addressBook = addressBookMapper.getDefaultAddressBook(userId);
        return R.success(addressBook);
    }



    //根据id查询地址
    @Override
    public R getAddressBookBYiId(Long id) {
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        AddressBook addressBook = addressBookMapper.getAddressBookById(id);
        return R.success(addressBook);
    }



    //设置默认地址
    @Override
    public R setDefaultAddressBook(AddressBook addressBook) {

        //获取当前默认地址id
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getIsDefault,1);
        AddressBook defaultAddressBook = addressBookMapper.selectOne(qw);
        //判断是否又默认地址
        if (defaultAddressBook != null){
            //设置原默认地址为0
            defaultAddressBook.setIsDefault(0);
            //修改
            addressBookMapper.updateById(defaultAddressBook);
        }
        //将新地址设为默认
        addressBook.setIsDefault(1);
        addressBookMapper.updateById(addressBook);
        return R.success("设置成功");
    }




    //新增地址
    @Override
    public R saveAddressBook(AddressBook addressBook) {
        //获取用户id
        Long userId = UserIdThreadLocal.getId();
        //设置id
        addressBook.setUserId(userId);
        //保存地址
        addressBookMapper.insert(addressBook);
        return R.success("保存地址成功");
    }
}
